/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smary.ejb.entity;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *Diese Entität enhtält alle Informationen ueber die Tasks, die Bennenung und
 * die Beschreibung als auch die Referenzen zu den Sprints(n zu 1), zu den Usern
 * (1 zu n) und der Userstory(n zu 1)
 * @author Alex
 */
@Entity
@Table(name = "TASK")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Task.findAll", query = "SELECT t FROM Task t"),
    @NamedQuery(name = "Task.findByTaskid", query = "SELECT t FROM Task t WHERE t.taskid = :taskid"),
    @NamedQuery(name = "Task.findByTaskbezeichnung", query = "SELECT t FROM Task t WHERE t.taskbezeichnung = :taskbezeichnung"),
    @NamedQuery(name = "Task.findByTaskbeschreibung", query = "SELECT t FROM Task t WHERE t.taskbeschreibung = :taskbeschreibung"),
    @NamedQuery(name = "Task.findByUserstory", query = "SELECT t FROM Task t WHERE t.userstory = :userstory"),
    @NamedQuery(name = "Task.findByPrioritaet", query = "SELECT t FROM Task t WHERE t.prioritaet = :prioritaet"),
    @NamedQuery(name = "Task.findBySprint", query = "SELECT t FROM Task t WHERE t.sprint = :sprint")})
public class Task implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "TASKID")
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer taskid;
    @Size(max = 2048)
    @Column(name = "TASKBEZEICHNUNG")
    private String taskbezeichnung;
    @Size(min = 1, max = 32700)
    @Column(name = "TASKBESCHREIBUNG")
    private String taskbeschreibung;
//    @Basic(optional = false)
//    @NotNull
//    @Column(name = "USERSTORYID")
//    private int userstoryid;
    @Basic(optional = false)
    @NotNull
    @Column(name = "PRIORITAET")
    private int prioritaet;
//    @Basic(optional = false)
//    @NotNull
//    @Column(name = "SPRINTID")
//    private int sprintid;
    
    @Basic(optional=false)
    @NotNull
    @Column(name="EFFORT")
    private int effort;

    @ManyToMany
    @JoinTable(name="Userloginaccount_Task",
            joinColumns=
                @JoinColumn(name="SS_TASKID",referencedColumnName="TASKID"),
            inverseJoinColumns=
            @JoinColumn(name="SS_LOGINID",referencedColumnName="LOGINID"))
    protected Set<Userloginaccount> users;

    public int getPrioritaet() {
        return prioritaet;
    }

    public void setPrioritaet(int prioritaet) {
        this.prioritaet = prioritaet;
    }

    public Set<Userloginaccount> getUsers() {
        return users;
    }

    public void setUsers(Set<Userloginaccount> users) {
        this.users = users;
    }
    
    //TODO: Benutzerreferenz
    
    public int getEffort() {
        return effort;
    }

    public void setEffort(int effort) {
        this.effort = effort;
    }

    @ManyToOne
    @JoinColumn(name="USERSTORYID",
            referencedColumnName="USERSTORYID")
    protected Userstory userstory;

    public Userstory getUserstory() {
        return userstory;
    }

    public void setUserstory(Userstory userstory) {
        this.userstory = userstory;
    }
    
    @ManyToOne
    @JoinColumn(name="SPRINTID",
            referencedColumnName="SPRINTID")
    protected Sprint sprint;

    public Sprint getSprint() {
        return sprint;
    }

    public void setSprint(Sprint sprint) {
        this.sprint = sprint;
    }
    
    @OneToMany(mappedBy="task")
    protected Set<Statusaenderung> statusaenderungen;

    public Set<Statusaenderung> getStatusaenderungen() {
        return statusaenderungen;
    }

    public void setStatusaenderungen(Set<Statusaenderung> statusaenderungen) {
        this.statusaenderungen = statusaenderungen;
    }
    
    public Task() {
    }

    public Task(Integer taskid) {
        this.taskid = taskid;
    }

    public Task(Integer taskid, Userstory userstoryid, int prioritaet, int sprintid) {
        this.taskid = taskid;
        this.userstory = userstoryid;
        this.prioritaet = prioritaet;
//        this.sprintid = sprintid;
    }

    public Integer getTaskid() {
        return taskid;
    }

    public void setTaskid(Integer taskid) {
        this.taskid = taskid;
    }

    public String getTaskbezeichnung() {
        return taskbezeichnung;
    }

    public void setTaskbezeichnung(String taskbezeichnung) {
        this.taskbezeichnung = taskbezeichnung;
    }

    public String getTaskbeschreibung() {
        return taskbeschreibung;
    }

    public void setTaskbeschreibung(String taskbeschreibung) {
        this.taskbeschreibung = taskbeschreibung;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (taskid != null ? taskid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Task)) {
            return false;
        }
        Task other = (Task) object;
        if ((this.taskid == null && other.taskid != null) || (this.taskid != null && !this.taskid.equals(other.taskid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getTaskid() + " " + getTaskbezeichnung();
    }
    
}
